const app = getApp()
var intervalId
Page({
  data: {
    userlists:[],
    userlists2:[],
    tohidden:true
  },
  onLoad() {
    var that=this
    app.comrequest('https://ipass.cc/myapi/getprizeapi.php',{num:4}).then(res=>{
      that.setData({
        userlists:res
      })
    })
  },
  btn1click(){
    var that=this
    var userlists=that.data.userlists
    var userlenght=userlists.length
    intervalId=setInterval(()=>{
      that.setData({
        randoms:Math.floor(Math.random()*userlenght)
      })
    },100)
  },
  btn2click(){
    var that=this
    clearInterval(intervalId)
    var randoms=that.data.randoms
    var userlists=that.data.userlists
    var userlists2=that.data.userlists2
    for(var i=0;i<userlists.length;i++){
      if(i==randoms){
        var userimg=userlists[i].userimg
        var username=userlists[i].username
        userlists2.push(userlists[i])
        userlists.splice(randoms,1)
        that.setData({
          userimg,
          username,
          tohidden:false
        })
        setTimeout(()=>{
          that.setData({
            tohidden:true
          })
        },2000)
      }
    }
    setTimeout(()=>{
      that.setData({
        userlists,
        userlists2,
        randoms:null,
      })
    },2000)
  }
})
